var database = require('../../data/data.js');
var app = getApp();
Page({
  data: {
    flag: true
  },
  onLoad: function (options) {
    var that = this;
    wx.getUserInfo({
      success(res) {
        console.log(res.userInfo.avatarUrl)
        that.setData({
          nickName: res.userInfo.nickName,
          avatarUrl: res.userInfo.avatarUrl,
          tel:database.tel
        })
      }
    })
    if (database.login) {
      wx.request({
        url: database.url + '/api/V2/user/get_user',
        method: "POST",
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          accessToken: database.accessToken,
          userId: database.userId
        },
        success(res) {
          database.tel = res.data.data.mobile
          that.setData({
            tel: database.tel
          })
          wx.request({
            url: database.url + '/api/V2/softball/softball_list',
            method: "POST",
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            data: {
              accessToken: database.accessToken,
              userId: database.userId
            },
            success(res) {
              that.setData({
                teams: res.data.data.softballList.length
              })
            }
          })
        }
      })
    } else {
      that.setData({
        tel: "未绑定"
      })
    }
    app.editTabBar();
  },
  toTeamList:function(){
    if(database.login){
      wx.navigateTo({
        url: '/pages/my/myteam/myteam/myteam',
      })
    }else{
      wx.navigateTo({
        url: '/pages/binding/binding',
      })
    }
  },
  toBingding:function(){
    wx.navigateTo({
      url: '/pages/binding/binding',
    })
  },
  toAbout: function () {
    wx.navigateTo({
      url: '/pages/my/about/about',
    })
  },
  add: function () {
    if (database.login) {
      if (!database.have_team) {
        this.setData({
          flag: false,
          prompt_img: "/img/gantan.png",
          prompt_text: "请先创建至少一支球队",
          prompt_bt1: "创建球队",
          prompt_bt2: "暂不创建",
          prompt_tap: "toCreate"
        })
      } else {
        wx.showToast({
          title: '您已参加该活动!',
        })
      }
    } else {
      this.setData({
        flag: false,
        prompt_img: "/img/gantan.png",
        prompt_text: "请先绑定手机",
        prompt_bt1: "绑定手机",
        prompt_bt2: "暂不绑定",
        prompt_tap: "toBinding"
      })
    }
  },
  hide: function () {
    this.setData({
      flag: true
    })
  },
  toBinding: function () {
    this.setData({
      flag: true
    })
    wx.navigateTo({
      url: '/pages/binding/binding',
    })
  },
  toCreate: function () {
    this.setData({
      flag: true
    })
    wx.navigateTo({
      url: '/pages/my/myteam/createteam/createteam',
    })
  },
  toGame:function(){
    wx.navigateToMiniProgram({
      appId: 'wx9471acc7a0c59a6e',
    })
  }
})